package GUI;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.Font;

@SuppressWarnings("serial")
/**
 * Update the logged-in user's card details
 * @author Benny
 *
 */
public class UpdateUserCardGUI extends JPanel {
	
	JTextField textName;
	public JTextField textID;
	public JTextField textEmail;
	public JTextField textPhone;
	public JTextField textOldPassword;
	public JTextField textNewPassword;
	public JTextField textConfirmNewPassword;
	public JLabel lblError;
	public MainFrame mf;
	
	/**
	 * initialize all the text fields of the GUI
	 */
	public void init(){
		textEmail.setText("");
		textPhone.setText("");
		textOldPassword.setText("");
		textNewPassword.setText("");
		textConfirmNewPassword.setText("");
		this.lblError.setVisible(false);
	}
	/**
	 * check for matching password validity
	 * @return validity status
	 */
	public int checkPass(){
		if(textNewPassword.getText().equals(textConfirmNewPassword.getText())) return 1;
		else return 0;
	}
	/**
	 * check for email validity
	 * @return validity status
	 */
	public int checkEmail(){
		String email = textEmail.getText();
		Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();

		if(matchFound) return 1;
		else return 0;
	}
	/**
	 * check for phone validity
	 * @return validity status
	 */
	public int checkPhone(){
        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
        CharSequence inputStr = textPhone.getText();
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches();

		if(matchFound) return 1;
		else return 0;
    }
	/**
	 * Create the panel.
	 * @param M Instance of the main Frame of the program 
	 */
	public UpdateUserCardGUI(MainFrame M) {
		setLayout(null);
		mf=M;

		JLabel lblViewUserCard = new JLabel("Update User Card");
		lblViewUserCard.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblViewUserCard.setBounds(225, 12, 221, 14);
		add(lblViewUserCard);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(138, 80, 151, 14);
		add(lblName);
		
		JLabel lblId = new JLabel("Id:");
		lblId.setBounds(138, 105, 151, 14);
		add(lblId);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(138, 130, 151, 14);
		add(lblEmail);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(138, 155, 151, 14);
		add(lblPhone);
		
		this.lblError = new JLabel("Details were updated successfuly");
		lblError.setForeground(Color.GREEN);
		lblError.setBounds(138, 198, 308, 15);
		lblError.setVisible(false);
		add(lblError);
		
		
		textName = new JTextField();
		textName.setEditable(false);
		textName.setBounds(320, 80, 126, 20);
		add(textName);
		textName.setColumns(10);
		
		textID = new JTextField();
		textID.setEditable(false);
		textID.setBounds(320, 105, 126, 20);
		add(textID);
		textID.setColumns(10);
		
		textEmail = new JTextField();
		textEmail.setBounds(320, 130, 126, 20);
		add(textEmail);
		textEmail.setColumns(10);
		
		textPhone = new JTextField();
		textPhone.setBounds(320, 155, 126, 20);
		add(textPhone);
		textPhone.setColumns(10);
		

		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				init();
			}
		});
		btnClear.setBounds(297, 361, 89, 23);
		add(btnClear);
		
		JLabel lblOldPassword = new JLabel("Old Password:");
		lblOldPassword.setBounds(138, 252, 151, 14);
		add(lblOldPassword);
		
		JLabel lblNewPassword = new JLabel("New Password:");
		lblNewPassword.setBounds(138, 277, 151, 14);
		add(lblNewPassword);
		
		JLabel lblConfirmNewPassword = new JLabel("Confirm New Password:");
		lblConfirmNewPassword.setBounds(138, 302, 151, 14);
		add(lblConfirmNewPassword);
		
		JLabel lblChangePassword = new JLabel("Change Password");
		lblChangePassword.setBounds(305, 225, 141, 14);
		add(lblChangePassword);
				
		textOldPassword = new JTextField();
		textOldPassword.setBounds(320, 249, 126, 20);
		add(textOldPassword);
		textOldPassword.setColumns(10);
		
		textNewPassword = new JTextField();
		textNewPassword.setBounds(320, 274, 126, 20);
		add(textNewPassword);
		textNewPassword.setColumns(10);
		
		textConfirmNewPassword = new JTextField();
		textConfirmNewPassword.setBounds(320, 299, 126, 20);
		add(textConfirmNewPassword);
		textConfirmNewPassword.setColumns(10);
		
		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Object> send = new ArrayList<>();
				Integer id=Integer.parseInt(textID.getText());
				send.add("1");
				if((textOldPassword.getText().equals("")) && (textNewPassword.getText().equals("")) && (textConfirmNewPassword.getText().equals("")))
				{
					if((checkEmail()==1) && (checkPhone()==1))
					{
						send.add("UpdateUserCard");
						send.add(id);
						if((!textEmail.getText().equals(""))&& (!textPhone.getText().equals("")))
						{
						send.add(textEmail.getText());
						send.add(textPhone.getText());
						mf.client.accept(send);
						}
					}
					else JOptionPane.showMessageDialog(null, "Incorrect Phone/Email", "Program", JOptionPane.ERROR_MESSAGE);
				}
				else 
				{
					send.add("UpdateCardPass");
					send.add(id);
					if((!textEmail.getText().equals("")) && (!textPhone.getText().equals("")))
					{
						if((checkEmail()==1) && (checkPhone()==1) && checkPass()==1)
						{
							send.add(textEmail.getText());
							send.add(textPhone.getText());
						}
						else JOptionPane.showMessageDialog(null, "Incorrect Phone/Email", "Program", JOptionPane.ERROR_MESSAGE);
					}
					send.add(textOldPassword.getText());
					send.add(textNewPassword.getText());
					send.add(textConfirmNewPassword.getText());
					mf.client.accept(send);
				}
			}
		});
		btnUpdate.setBounds(192, 361, 89, 23);
		add(btnUpdate);
		
		ImageIcon img = new ImageIcon(getClass().getResource(
                "/util/grad2.jpg"));
		JLabel picLabel = new JLabel(img);
		picLabel.setLocation(0, 0);
		picLabel.setSize(696, 610);
		add(picLabel);
	}
}
